/**
 * 
 */
package de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation;
import java.io.BufferedReader;
import java.io.IOException;
/**
 * 
 * @author Christoph Teichmann
 * created Jun 7, 2014 3:16:33 PM
 * @version 0.1
 */
public class CSVTokenizer implements Tokenizer
{
	/**
	 * @param downCase
	 */
	public CSVTokenizer(boolean downCase)
	{this.downCase = downCase;}
	/**
	 * 
	 */
	private final boolean downCase;
	/**
	 * 
	 */
	int pos = 0;
	/**
	 * 
	 */
	private String[] languages;
	/**
	 * 
	 */
	private String[] next;
	/**
	 * 
	 */
	private BufferedReader br;
	/* (non-Javadoc)
	 * @see de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer#readFrom(java.io.BufferedReader)
	 */
	@Override
	public void readFrom(BufferedReader source) throws IOException
	{
		this.br = source;
		this.languages = this.br.readLine().split("\\t+");
		for(int i=0;i<languages.length;++i)
		{this.languages[i] = new String(this.languages[i].trim());}
		makeNext();
	}
	/**
	 *
	 * @throws IOException
	 */
	private void makeNext() throws IOException
	{
		String line = br.readLine();
		if(line == null)
		{
			this.next = null;
			return;
		}
		if(this.downCase)
		{line = line.toLowerCase();}
		if(line != null)
		{
			this.next = line.split("\\t+");
			for(int i=0;i<this.next.length;++i)
			{this.next[i] = new String(this.next[i].trim());}
		}
		else
		{this.next = null;}
		this.pos = -1;
		while(this.hasNext() && this.next[this.pos+1].equals(""))
		{++this.pos;}
	}
	/* (non-Javadoc)
	 * @see de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer#hasNext()
	 */
	@Override
	public boolean hasNext() throws IOException
	{
		if(this.next == null)
		{return false;}
		if(this.pos < this.next.length-1)
		{return true;}
		else
		{this.makeNext();}
		return this.hasNext();
	}
	/* (non-Javadoc)
	 * @see de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer#currentWord()
	 */
	@Override
	public String currentWord()
	{return this.next[this.pos];}
	/* (non-Javadoc)
	 * @see de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer#currentLanguage()
	 */
	@Override
	public String currentLanguage()
	{return this.languages[this.pos];}
	/* (non-Javadoc)
	 * @see de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer#setToNext()
	 */
	@Override
	public void setToNext() throws IOException
	{
		while(this.hasNext() && this.next[this.pos+1].equals(""))
		{++this.pos;}
		++this.pos;
	}
}